//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-5-18
// @Time:上午11:34
//

#include <iostream>
#include "SerialClient.h"
#include "TcpClient.h"

void runSerialFunction(SerialClient *client, boost::asio::serial_port &port, const boost::system::error_code &ec,
                       size_t byte_read)
{
    std::string msg_data(&client->getMBuf()[0], byte_read);

    std::cout << " read : " << msg_data << " byte_read : " << byte_read << std::endl;

    // 循环
    client->read_buffer(runSerialFunction);
}


int main(int argc, char *argv[])
{

//    int port = 8080;
    if (argc < 1)
    {
        printf("usage : %s [serial_prot] [serverip] [serverport]", argv[0]);
        return 0;
    }

    std::string portName(argv[1]);

    std::string serverip(argv[2]);

    int portServer = atoi(argv[3]);

    try
    {
        TcpClient tcpClient(serverip, portServer);
        SerialClient serialClient(portName);


        std::thread serialTh([&serialClient]()
                             {
                                 std::cout << " run " << std::endl;
                                 serialClient.read_buffer(runSerialFunction);
                                 serialClient.run();
                                 std::cout << " stop " << std::endl;
                             });


        tcpClient.setReacCallback([](const TcpClient *client, std::string &message)
                                  {
                                      std::cout << " read tcp :" << message << std::endl;
                                  });
        std::thread clientTh([&tcpClient]()
                             {
                                 tcpClient.run();
                             });


        std::thread inputCmd([&serialClient, &tcpClient]()
                             {
                                 while (true)
                                 {
                                     std::cin.clear();
                                     std::cout << "input : ";
                                     std::string str;
                                     getline(std::cin, str);
                                     if (str == "tran")
                                     {
                                         //tranToServer = true;
                                     } else if (str == "untr")
                                     {
                                         //tranToServer = false;
                                     } else if (str == "quit")
                                     {
                                         tcpClient.stop();
                                         serialClient.stop();
                                         break;
                                     } else
                                     {
                                         serialClient.write(str);
                                     }
                                 }
                             });

        clientTh.join();
        serialTh.join();
        inputCmd.join();


    } catch (std::exception &e)
    {
        std::cout << "error : " << e.what() << std::endl;
    }
    std::cout << "client end." << std::endl;
}